package com.sservice.commonBusiness.controller.web;

import com.sservice.base.entity.dto.ListResult;
import com.sservice.commonBusiness.e.DataR;
import com.sservice.commonBusiness.service.DataRService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by byte_su@163.com on 2018/01/12.
 */
@Controller
@RequestMapping("/dataR1/")
public class DataR1Controller {

    @Autowired
    DataRService dataRService;


    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public ListResult add(DataR dataR) {
        List<DataR> list = new ArrayList();
        list.add(dataR);
        return new ListResult(true, dataRService.addBatch(list));
    }
    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    public ListResult del(DataR dataR) {
        return new ListResult(true, dataRService.delBy(dataR));
    }

    /**
     * 增加唯一关系
     *
     * @param dataR
     * @return
     */
    @RequestMapping(value = "addUnique", method = RequestMethod.POST)
    @ResponseBody
    public ListResult addUnique(DataR dataR) {
        DataR dataR1 = new DataR();
        dataR1.setTargetId(dataR.getTargetId());
        dataR1.setSourceId(dataR.getSourceId());
        dataR1.setType1(dataR.getType1());
//        dataR1.setDesc1(dataR.getDesc1());
        List<DataR> list1 = dataRService.selectBy(dataR1);
        if (list1.size() > 0) {
            return new ListResult(false, list1.get(0).getId().toString(), 406);//重复
        } else {
            List<DataR> list = new ArrayList();
            list.add(dataR);
            return new ListResult(true, dataRService.addBatch(list));
        }
    }

    /**
     * 条件查询
     *
     * @param dataR
     * @return
     */
    @RequestMapping("selectBy")
    @ResponseBody
    public ListResult list(DataR dataR) {
        List<DataR> list = dataRService.selectBy(dataR);
        return new ListResult(true, list);
    }
}
